//
//  PayCell.swift
//  Aiwei
//
//  Created by zhu xietong on 2017/11/9.
//  Copyright © 2017年 zhu xietong. All rights reserved.
//

import Foundation
import Eelay
import soapp
// import mosdks

class PayKit: FOptionV {
    
    let iconV = UIImageView()
    let line = UIView()
    override func setOptionLayout() {
        self.edeg = UIEdgeInsets(top: 2, left: 6, bottom: 2, right: 6)
        eelay = [
            [iconV,[ee.Y.L,[0.0]],"22",22],
            [titleL,[ee.Y],[iconV,ee.R,ee.L,8]],
            [imgV,[ee.R.Y],"18",18],
            [line,[ee.L.B.R],"\(TP.separatorHeight)"],
            [bt,[ee.T.L.B.R]],
        ]
//        bt.setContentConstrainToLowLever()
        imgV.setImage(UIImage(named:"radio_check_n"), for: .normal)
        imgV.setImage(UIImage(named:"radio_check_s"), for: .selected)
        backgroundColor = .clear
        iconV.setContentConstrainToLowLever()
        line.backgroundColor = UIColor._separator
        self.bsui.set(radius: (1), width: nil, color: nil)
        _ = titleL.ui.center.font13.text(color: .color(light: "#666", dark: "#aaa"))
        bt.setImage(nil, for: .normal)
        bt.setImage(nil, for: .selected)

    }
    
    @objc open override func tag_touched()
    {
        
        imgV.isSelected = !imgV.isSelected
        self.pressedAction(self.tag,imgV.isSelected,self.mo.model,self)
        mo_model_reload()
    }
    
    open override var intrinsicContentSize: CGSize
    {
        let s_w = Swidth - (2.0 * AppTheme.edge)
        return [s_w,36]
    }
    
    override func mo_model_reload() {
        iconV.img_name = mo.model["icon",""]
        titleL.text = mo.model["name",""]

        if mo.model[FK.selected,"NO"] == "YES"
        {
            imgV.isSelected =  true
        }
        else
        {
            imgV.isSelected = false
        }
        
        if mo.model["__last","NO"] == "YES"
        {
            line.alpha =  0
        }
        else
        {
            line.alpha =  1
        }
        backgroundColor = .clear
    }
}


class SimplePayCell: EeFieldsCell {
    let field = SimplePayField()
    override func addLayoutRules() {
        eelay = [
            [field,[ee.X],[ee.T.B,[0,20]],"30"],
        ]
        fields = [field]
    }
}

class SimplePayField: FieldView {
    let valueL = UILabel()
    let changeBt = UIButton()
    override func didLayoutRules() {
        self.defaultField = StrField(id: "paytype", defaultV: nil, need: false, hiden: false, rule:
            JoRule(reg: "^[\\s\\S]{0,200}$", nil_msg: "检查支付方式".loc, err_msg: "请检支付方式".loc))
        changeBt.addTarget(self, action: #selector(changeType), for: .touchUpInside)
        valueL.textAlignment = .center

    }
    
    @objc func changeType() {
        let alert = JoAlertView(info: "请选择支付方式".loc)
        let options = PayMethod.payOptions
        for one in options
        {
            let dict = one.mutable_dictionary
            alert.append(title: dict["name",""]) { [weak self] () in
                let value = dict["id",""]
                self?.mo.model[FK.value] = value
                self?.mo_model_reload()
                self?.save(value)
            }
        }
        alert.append(title: "取消".loc, style: .cancel) {
            
        }
        alert.sheet.show(at: controller)
    }
    
    override func mo_model_reload() {
        var value = mo.model[FK.value,""]
        if value.len < 1{
            value = PayMethod.defaultMethod.rawValue
        }
        if let strfield = mo.model[FK.field] as? String
        {
            self.defaultField?._ID = strfield
            self.mo.model[FK.field] = self.defaultField
        }
        
 
        
        mo.model[FK.value] = value
        if let type = PayMethod(rawValue: value)
        {
            valueL.attributedText = "\(type.name)".ui.font11.color(color: ._foreground).astring + "更改".loc.ui.center.font12.color(color: ._tint).astring
        }
        else{
            valueL.attributedText = "选择支付方式".loc.ui.font11.center.color(color: ._tint).astring
        }
    }
    
    override func addLayoutRules() {
        eelay = [
            [valueL,[ee.X],[ee.Y,0.+1000]],
            [changeBt,[ee.T.L.B.R]],
//            [changeBt,[valueL,ee.R,ee.L],[ee.Y],[ee.R]],
        ]
//        changeBt.backgroundColor = .red
    }
}




class PayCell<Kit:FOptionV>: FieldCell {
    var field1 = TagsOptionField<Kit>()
    
    override func addLayoutRules() {
        titleL.ui.font13.text(color: ._foreground).node("title")
        let w = Int(FieldCell.name_width)
        
        let whiteBK = UIView()

        contentView.eelay = [
            [titleL,[ee.T.L,[12,AppTheme.edge]],"21",.<w],
            [requiredMark,[titleL,ee.Y,3],[titleL,ee.R,ee.L,2]],
            [whiteBK,[titleL,ee.B,ee.T,10],[ee.B,-10],[ee.R.L]],
        ]
        field1.backgroundColor = .clear
        whiteBK.backgroundColor = ._cellBackground
        
        self.indicator = .none
        titleL.setContentConstrainToHighLever()
        requiredMark.ui.font18.text("*").text(color: .clear)
        rightIcon.ui.font(size: 13).ncolor(color: .color(light: "#aaa", dark: "#8884"))
        whiteBK.eelay = [
            [field1,[ee.X],[ee.T.B,[10,-10]]],
        ]
        field1.tagsV.preferredMaxLayoutWidth = Swidth - 2*AppTheme.edge
        field1.insets = 0
        field1.lineSpace = 0
    
//        field1.backgroundColor = UIColor(shex:"#ccc")
        contentView.backgroundColor = .clear
        self.backgroundColor = .clear
    }
    
    
    override func mo_model_reload() {
        titleL.text = mo.model["title",""]
        if let f = DataNode<NSMutableDictionary>.path(node: "\(FK.field)", mo.model)
        {
            let list = DataNode.path(node: FK.options, f, value: NSMutableArray())
            if let last = list.lastObject as? NSMutableDictionary
            {
                last["__last"] = "YES"
            }
            field1.mo.model = f
        }
        else
        {
            if let f = DataNode<NSMutableDictionary>.path(node: "\(FK.fields).0", mo.model)
            {
                let list = DataNode.path(node: FK.options, f, value: NSMutableArray())
                if let last = list.lastObject as? NSMutableDictionary
                {
                    last["__last"] = "YES"
                }
                field1.mo.model = f
            }
        }
    }
    
}



